home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C011.ZIP / BSPLIT.C < prev    next >
Text File  |  1990-01-19  |  4KB  |  151 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.011        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: bsplit.c
  7.  * Program name: bcombine
  8.  * Source of file: written by Martin Houston
  9.  * Purpose: 
  10.  *
  11.  * bsplit: a Binary version of the Unix split command.
  12.  * Instead of working on text lines like split does bsplit will
  13.  * fragment the input file into evenley sized pieces (except for
  14.  * any left over from the input).
  15.  *
  16.  * bsplit [-nbytes] [file [name]]
  17.  *
  18.  * - can be given for file if standard input is to be used
  19.  * bytes is taken as a long decimal value
  20.  * name is the prefix used for the files created.
  21.  * The first file created will be {name}aa, the second {name}ab and so on.
  22.  *
  23.  * Changes: <who what when & why major changes have been made>      
  24.  */
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27.  
  28. #define CHUNKSIZE 10240L    /* 10K default for n */
  29. extern long atol();
  30.  
  31. /*
  32.  * Get the next name in the series.
  33.  */
  34. static char *namegen(seed)
  35. char *seed;
  36. {
  37.     static int count = 0;
  38.     static char1 = 'a';
  39.     static char2 = 'a';
  40.     char result[40];
  41.  
  42.     if(strlen(seed) > (sizeof(result) - 2))
  43.         seed[sizeof(result) - 2] = 0; /* truncate so result will fit */
  44.  
  45.     sprintf(result,"%s%c%c", seed, char1, char2);
  46.     if(++char2 > 'z')
  47.     {
  48.         char2 = 'a';
  49.         if(++char1 > 'z')
  50.         {
  51.         fprintf(stderr,"bsplit has run out of file names!\n");
  52.         exit(0);
  53.         }
  54.     }
  55.  
  56.     return result;
  57. }
  58.  
  59. main(argc, argv)
  60. int argc;
  61. char **argv;
  62. {
  63.  
  64.     int fdesc = 0;
  65.     long filesize = CHUNKSIZE;
  66.     char *filename = "x";    /* default of no name specified */
  67.  
  68.     if(argc > 1)
  69.     {
  70.         if((argv[1][0] == '-') && (argv[1][1] == 'n'))
  71.         {
  72.             filesize = atol(&(argv[1][2]));
  73.             argv++;    /* move count on */
  74.             argc--;
  75.         }
  76.         if(argc > 1)
  77.         {
  78.             if(argv[1][0] == '-')
  79.             fdesc = dup(0);
  80.             else
  81.                 fdesc = open(argv[1], O_RDONLY);
  82.             if(fdesc < 0)
  83.             {
  84.             fprintf(stderr,"bsplit cannot open %s\n", argv[1]);
  85.             exit(0);
  86.             }
  87.             argv++;    /* move count on */
  88.             argc--;
  89.         }
  90.         if(argc > 1)
  91.             filename = argv[1];
  92.     }
  93.  
  94.     splitit(fdesc, filesize, filename);
  95. }
  96.  
  97. splitit(input, chunksize, seed)
  98. int input;
  99. long chunksize;
  100. char *seed;
  101. {
  102.     static char buf[20480];    /* io buffer */
  103.     char *outname;
  104.     int rcnt, wcnt;
  105.     int outfd;
  106.     long byteswritten = 0L;
  107.     char boundflag;
  108.  
  109.     outname = namegen(seed);
  110.     if((outfd = open(outname, O_WRONLY| O_CREAT, 0666)) < 0)
  111.     {
  112.         fprintf(stderr,"bsplit cannot open %s\n", outname);
  113.         exit(0);
  114.     }
  115.     do
  116.     {
  117.         if(byteswritten >= chunksize)
  118.         {
  119.         close(outfd);
  120.             outname = namegen(seed);
  121.             if((outfd = open(outname, O_WRONLY| O_CREAT, 0666)) < 0)
  122.             {
  123.                 fprintf(stderr,"bsplit cannot open %s\n", outname);
  124.                 exit(0);
  125.             }
  126.         byteswritten = 0L;
  127.             }
  128.         boundflag = 0;
  129.         if((chunksize - byteswritten) < (long)sizeof(buf))
  130.         rcnt = (int)(chunksize - byteswritten);
  131.         else
  132.         rcnt = sizeof(buf);
  133.         if(rcnt > 0)
  134.         rcnt = read(input, buf, rcnt);
  135.         else
  136.         boundflag = 1; /* run out of data */
  137.         if(rcnt > 0)
  138.         {
  139.             wcnt = write(outfd, buf, rcnt);
  140.         if(wcnt != rcnt)
  141.             {
  142.                 fprintf(stderr,"bsplit write failure on %s\n", outname);
  143.                 exit(0);
  144.             }
  145.         }
  146.         byteswritten += (long)wcnt;
  147.     }
  148.     while(boundflag || rcnt > 0);
  149. }
  150.  
  151.